﻿/*****************************************介绍*****************************************
 * 作者：24531
 * 创建时间：2025-09-14 21:19:15
 * 功能：单个记录对象
**************************************************************************************/

using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.HallPanel
{
    public class RecordItem : MonoBehaviour
    {
        [SerializeField] Color[] m_StatusColors;
        [SerializeField] Sprite m_DefaultIcon;
        private Image m_Bg, m_Icon;
        private GameObject m_Dim, m_Mask, m_Stars;
        private Transform m_LvStatus;
        private Text m_Lv, m_Status;
        private Button m_EnterBtn;
        private GameDataModel.Level m_Data;
        private HallPanelView m_RootView;

        private void Find()
        {
            m_Bg = transform.Find("LvBtn").GetComponent<Image>();
            m_EnterBtn = m_Bg.GetComponent<Button>();
            m_Mask = transform.Find("LvBtn/Mask").gameObject;
            m_Icon = m_Mask.transform.Find("Icon").GetComponent<Image>();
            m_Dim = transform.Find("LvBtn/Dim").gameObject;
            m_LvStatus = transform.Find("LvBtn/LvInfoStatus");
            m_Lv = m_LvStatus.Find("Level").GetComponent<Text>();
            m_Status = transform.Find("LvBtn/Status").GetComponent<Text>();
            m_Stars = transform.Find("Stars").gameObject;
            m_EnterBtn.onClick.AddListener(EnterBtnClick);
        }

        private void EnterBtnClick()
        {
            // 这块判断是否需要进行选择，如果不需要则直接进入游戏
            if (m_Data.status == ELevelStatus.Completed)
                m_Data.InitItems();

            if (m_Data.level > 1 && m_Data.finalSelectIdx == -1)
                UIUtility.DisplayPanel(ChoosePanel.ChoosePanelView.PANEL_KEY, "UI/ChoosePanel/Prefabs/ChoosePanel", data: m_Data);
            else
            {
                UIUtility.DisplayPanel(GameLoadingPanel.GameLoadingPanelView.PANEL_KEY, "UI/GameLoadingPanel/Prefabs/GameLoadingPanel", data: m_Data);
                m_RootView.CutHomePage();
            }
        }

        public void Init(GameDataModel.Level data, int lv, HallPanelView rootView)
        {
            m_RootView = rootView;
            if (m_Mask == null) Find();
            m_Data = data;

            // 更新UI状态
            ELevelStatus status = data == null ? ELevelStatus.Locked : data.status;
            m_EnterBtn.interactable = status != ELevelStatus.Locked;

            for (int i = 0; i < 3; i++)
                m_LvStatus.GetChild(i).gameObject.SetActive(false);

            switch (status)
            {
                case ELevelStatus.Locked:
                    m_LvStatus.GetChild(0).gameObject.SetActive(true);
                    m_Status.text = BaseDataMgr.Instance.GetLanguageValue("Locked");
                    m_Status.color = m_StatusColors[0];
                    m_Lv.color = m_StatusColors[0];
                    break;
                case ELevelStatus.Unlocked:
                    m_LvStatus.GetChild(1).gameObject.SetActive(true);
                    m_Status.color = m_StatusColors[1];
                    m_Lv.color = m_StatusColors[1];
                    m_Status.text = BaseDataMgr.Instance.GetLanguageValue("Unlocked");
                    break;
                case ELevelStatus.InProcess:
                    m_LvStatus.GetChild(1).gameObject.SetActive(true);
                    m_Status.color = m_StatusColors[1];
                    m_Lv.color = m_StatusColors[1];
                    m_Status.text = BaseDataMgr.Instance.GetLanguageValue("In Progress");
                    break;
                case ELevelStatus.Completed:
                    m_LvStatus.GetChild(2).gameObject.SetActive(true);
                    m_Status.color = m_StatusColors[2];
                    m_Lv.color = m_StatusColors[2];
                    m_Status.text = BaseDataMgr.Instance.GetLanguageValue("Completed");
                    break;
            }
            m_Lv.text = $"{lv}";
            m_Bg.color = status == ELevelStatus.Unlocked ? new Color32(163, 237, 61, 255) : Color.white;

            m_Dim.SetActive(status == ELevelStatus.Locked);
            m_Mask.SetActive(status != ELevelStatus.Locked);
            // 处理按钮图片，如果已解锁则显示默认模糊图，如果进行中，则显示选择的那张图
            if (status != ELevelStatus.Locked)
            {
                if (m_Data.level == 1)
                {
                    m_Icon.sprite = AssetCacheServer.Instance.GetDefaultBg();
                    var size = UIUtility.CalcSizeToFill(m_Mask.transform as RectTransform, m_Icon.sprite.texture);
                    m_Icon.rectTransform.sizeDelta = new Vector2(size.width, size.height);
                }
                else if (status == ELevelStatus.Unlocked || m_Data.finalSelectIdx == -1)
                {
                    m_Icon.sprite = m_DefaultIcon;
                    m_Icon.SetNativeSize();
                }
                else
                {
                    var info = m_Data.assetsInfo[m_Data.finalSelectIdx];
                    var sprite = AssetCacheServer.Instance.GetSprite(info.type, info.id);
                    if (sprite == null)
                    {
                        m_Icon.sprite = m_DefaultIcon;
                        m_Icon.SetNativeSize();
                    }
                    else
                    {
                        m_Icon.sprite = sprite;
                        var size = UIUtility.CalcSizeToFill(m_Mask.transform as RectTransform, sprite.texture);
                        m_Icon.rectTransform.sizeDelta = new Vector2(size.width, size.height);
                    }
                }
            }

            // 处理星星显示
            m_Stars.SetActive(status != ELevelStatus.Locked);
            if (status != ELevelStatus.Locked)
            {
                int count = (status == ELevelStatus.Unlocked || status == ELevelStatus.InProcess) ? 0 : (m_Data.level >= BaseDataMgr.Instance.Cfg.props.error.unlock ? m_Data.lifeRemainNum : 3);
                for (int i = 0; i < 3; i++)
                    m_Stars.transform.GetChild(i).GetChild(0).gameObject.SetActive(i < count);
            }
        }

        public void Clear()
        {
            m_Data = null;
        }
    }
}